package com.xumt.hwHMSdemo;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Switch;
import android.widget.Toast;

import com.xumt.hwHMSdemo.cannectSb.HttpRequest;

public class updatamember extends AppCompatActivity {
    private String isvip=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_updatamember);
        Button button=(Button)findViewById(R.id.ifupdata);
        checkvip();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        init();
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                updata();
            }
        });
    }
    public void updata() {
        if(isvip.equals("false")){ AlertDialog alertDialog2 = new AlertDialog.Builder(this)
                .setTitle("升级会员")
                .setMessage("是否花费XX升级成为会员")
                .setIcon(R.mipmap.ic_launcher)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {//添加"Yes"按钮
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        new Thread() {
                            public void run() {
                                try {
                                    HttpRequest request = new HttpRequest();
                                    request.sendGet("http://169.254.67.221:8081/user/updatevip", "username="+Login2.usernamen  );
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                        }.start();
                        Toast.makeText(updatamember.this, "恭喜成为会员", Toast.LENGTH_SHORT).show();
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        refresh();
                    }
                })

                .setNegativeButton("取消", new DialogInterface.OnClickListener() {//添加取消
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                        Toast.makeText(updatamember.this, "您已取消交易", Toast.LENGTH_SHORT).show();
                    }
                })
                .create();
            alertDialog2.show();}
        else {
            AlertDialog alertDialog1 = new AlertDialog.Builder(this)
                    .setTitle("升级会员")//标题
                    .setMessage("您已经是会员啦")//内容
                    .setIcon(R.mipmap.ic_launcher)//图标
                    .create();
            alertDialog1.show();
        }

    }

    public void init(){
        Switch aSwitch = (Switch) findViewById(R.id.switch1);
        if(isvip.equals("true"))aSwitch.setChecked(true);
        else aSwitch.setChecked(false);
    }
    public void checkvip(){
        new Thread() {
            public void run() {
                try {
                    HttpRequest request = new HttpRequest();
                    isvip = request.sendGet("http://169.254.67.221:8081/user/isvip", "username="+Login2.usernamen  );
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }
    public void refresh() {
        finish();
        Intent intent = new Intent(updatamember.this, updatamember.class);
        startActivity(intent);
    }
}